package com.base.mybaseproject.demo;

import java.io.File;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.DownloadManager;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ProgressBar;

import com.base.mybaseproject.base.BaseActivity;
import com.base.mybaseproject.base.Constant;
import com.base.mybaseproject.imageLoader.impl.FileUtils;
import com.base.mybaseproject.utils.DownloadManagerPro;
import com.example.mybaseproject.R;

@SuppressLint("HandlerLeak")
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class DownloadActivity extends BaseActivity {
	DownloadManager dm;
	DownloadManagerPro dmp;
	Long id;
	ProgressBar pb;
	MyHandler handler;
	DownLoadChanged dco;
	String zipPath = "My" + File.separator + "download";

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.demo_download_activity);

		pb = (ProgressBar) findViewById(R.id.demo_download_pb);
		dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
		dmp = new DownloadManagerPro(dm);
		handler = new MyHandler();
		dco = new DownLoadChanged();

		DownloadManager.Request request = new DownloadManager.Request(Uri.parse(Constant.DOWN_TEST));
		File folder = new File(Constant.BASE_PATH + "download");
		if (!folder.exists() || folder.isDirectory()) {
			folder.mkdirs();
		}
		request.setDestinationInExternalPublicDir(zipPath, FileUtils.getFileName(Constant.DOWN_TEST));
		request.setTitle("可爱可亲");
		id = dm.enqueue(request);

		// DownloadManager.Query query = new DownloadManager.Query();
	}

	class DownLoadChanged extends ContentObserver {

		public DownLoadChanged() {
			super(handler);
		}

		@Override
		public void onChange(boolean selfChange) {
			super.onChange(selfChange);
			refreshView();
		}

	}

	private void refreshView() {
		int[] bytesAndStatus = dmp.getBytesAndStatus(id);
		handler.sendMessage(handler.obtainMessage(0, bytesAndStatus[0], bytesAndStatus[1], bytesAndStatus[2]));
	}

	private class MyHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				int status = (Integer) msg.obj;
				if (DownloadManagerPro.isDownloading(status)) {
					pb.setVisibility(View.VISIBLE);
					pb.setMax(0);
					pb.setProgress(0);
					if (msg.arg2 < 0) {
						pb.setIndeterminate(true);
					} else {
						pb.setIndeterminate(false);
						pb.setMax(msg.arg2);
						pb.setProgress(msg.arg1);
					}

				} else {
					if (status == DownloadManager.STATUS_FAILED) {

					} else if (status == DownloadManager.STATUS_SUCCESSFUL) {
						com.support.Utils.showToast(DownloadActivity.this, "success");
					}
				}
				break;
			}
			super.handleMessage(msg);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		getContentResolver().registerContentObserver(DownloadManagerPro.CONTENT_URI, true, dco);
		refreshView();
	}

	@Override
	protected void onPause() {
		super.onPause();
		getContentResolver().unregisterContentObserver(dco);
	}

}
